package com.xmh.code.generation.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
public class DateUtil {

    private static final ThreadLocal<SimpleDateFormat> SDF_THREADLOCAL = new ThreadLocal<>();

    public final static String PATTERN = "yyyy-MM-dd HH:mm:ss";

    public final static String PATTERN_SIMPLIFY = "yyyy-MM-dd";

    private static Date LAST_DATE = null;

    private static String path = PropertiesUtil.getFilePath();

    public static Date[] getStratAndEndDate() {
        Date endDate = new Date();

        Date startDate = null;

        long time = 1000L * 60L * 60L * 2L;

        if (LAST_DATE == null) {
            if (FileUtil.isExist(path)) {
                startDate = new Date(FileUtil.getFile(path).getTime() - time);
            } else {
                startDate = new Date(endDate.getTime() - time);
            }
        } else {
            startDate = new Date(LAST_DATE.getTime() - time);
        }

        LAST_DATE = endDate;

        FileUtil.outFile(LAST_DATE, path);

        return new Date[]{startDate, endDate};
    }

    public static void setLAST_DATE(Date lAST_DATE) {
        LAST_DATE = lAST_DATE;
    }

    public static SimpleDateFormat getSimpleDateFormat() {
        SimpleDateFormat sdf = SDF_THREADLOCAL.get();

        if (sdf == null) {
            sdf = new SimpleDateFormat(PATTERN);
            SDF_THREADLOCAL.set(sdf);
        }

        return sdf;
    }

    public static String getDateToString(Date date) {
        SimpleDateFormat simpleDateFormat = getSimpleDateFormat();

        simpleDateFormat.applyPattern(PATTERN);

        return simpleDateFormat.format(date);
    }

    public static String getDateToString(Date date, String pattern) {
        SimpleDateFormat simpleDateFormat = getSimpleDateFormat();

        simpleDateFormat.applyPattern(pattern);

        return simpleDateFormat.format(date);
    }

    public static Date getStringToDate(String time) {
        try {
            if (StringUtils.isEmpty(time)) {
                return null;
            }
            SimpleDateFormat simpleDateFormat = getSimpleDateFormat();

            simpleDateFormat.applyPattern(PATTERN);

            return simpleDateFormat.parse(time);
        } catch (ParseException e) {
            log.error("时间转换为出现异常，要转换为date的string对象为 " + time, e);
        }
        return null;
    }

    public static Date getStringToDate(String time, String pattern) {
        try {
            if (StringUtils.isEmpty(time) || StringUtils.isEmpty(pattern)) {
                return null;
            }

            SimpleDateFormat simpleDateFormat = getSimpleDateFormat();

            simpleDateFormat.applyPattern(pattern);

            return simpleDateFormat.parse(time);
        } catch (ParseException e) {
            log.error("时间转换为出现异常，要转换为date的string对象为 " + time + "   pattern为：   " + pattern, e);
        }
        return null;
    }

    public static Date stringToDate(String time) {
        try {
            if (StringUtils.isEmpty(time)) {
                return null;
            }
            time = time.trim();

            SimpleDateFormat simpleDateFormat = getSimpleDateFormat();

            if (time.length() <= 11) {
                simpleDateFormat.applyPattern(PATTERN_SIMPLIFY);
            } else {
                simpleDateFormat.applyPattern(PATTERN);
            }

            return simpleDateFormat.parse(time);
        } catch (ParseException e) {
            log.error("时间转换为出现异常，要转换为date的string对象为 " + time, e);
        }
        return null;

    }

}
